package com.ruoyi.airspaceFlight.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.airspaceFlight.domain.LowairServiceFile;
import com.ruoyi.airspaceFlight.domain.TLowairServiceFile;

import com.ruoyi.airspaceFlight.dto.req.AirspaceApplyAndFileDto;
import com.ruoyi.airspaceFlight.dto.resp.AirspaceApplyAndFileVo;
import com.ruoyi.airspaceFlight.mapper.TLowairServiceFileMapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.airspaceFlight.mapper.AirspaceApplyMapper;
import com.ruoyi.airspaceFlight.domain.AirspaceApply;
import com.ruoyi.airspaceFlight.service.IAirspaceApplyService;

/**
 * 空域申请Service业务层处理
 * 
 * @author zc
 * @date 2023-11-04
 */
@Service
public class AirspaceApplyServiceImpl implements IAirspaceApplyService 
{
    @Autowired
    private AirspaceApplyMapper airspaceApplyMapper;

    
    
    @Autowired
    private TLowairServiceFileMapper tLowairServiceFileMapper;


    /**
     * 查询空域申请
     * 
     * @param airspaceApplyId 空域申请主键
     * @return 空域申请
     */
    @Override
    public AirspaceApplyAndFileVo selectAirspaceApplyByAirspaceApplyId(String airspaceApplyId)
    {
        //空域详情表注入
        AirspaceApplyAndFileVo airspaceApplyAndFileVo = new AirspaceApplyAndFileVo();
        AirspaceApply airspaceApply = airspaceApplyMapper.selectAirspaceApplyByAirspaceApplyId(airspaceApplyId);
        airspaceApplyAndFileVo.setAirspaceApply(airspaceApply);
        //文件表注入
        //根据id查所有的文件
        LambdaQueryWrapper<TLowairServiceFile> qw = new LambdaQueryWrapper<>();
        qw.eq(  null != airspaceApply.getAirspaceApplyId(),
                TLowairServiceFile::getBusinessId, airspaceApply.getAirspaceApplyId());
        List<TLowairServiceFile> tLowairServiceFiles = tLowairServiceFileMapper.selectList(qw);

        airspaceApplyAndFileVo.setTlowairServiceFiles(tLowairServiceFiles);

        return airspaceApplyAndFileVo;
    }

    /**
     * 查询空域申请列表
     * 
     * @param airspaceApply 空域申请
     * @return 空域申请
     */
    @Override
    public List<AirspaceApply> selectAirspaceApplyList(AirspaceApply airspaceApply)
    {
        return airspaceApplyMapper.selectAirspaceApplyList(airspaceApply);
    }

    /**
     * 新增空域申请
     * 
     * @param airspaceApplyAndFileDto 空域申请+文件
     * @return 结果
     */
    @Override
    public int insertAirspaceApply(AirspaceApplyAndFileDto airspaceApplyAndFileDto)
    {
        //空域申请表的添加
        AirspaceApply airspaceApply = airspaceApplyAndFileDto.getAirspaceApply();


        //添加空域申请编号
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = dateFormat.format(date);
        airspaceApply.setAirspaceApplyId(IdUtil.randomUUID());
        String AirspaceApplyCode = "KY"+dateString+ RandomUtil.randomString(4);
        airspaceApply.setAirspaceApplyCode(AirspaceApplyCode);

        airspaceApply.setUpdateTime(DateUtils.getNowDate());
        airspaceApply.setCreateTime(DateUtils.getNowDate());
        int i = airspaceApplyMapper.insertAirspaceApply(airspaceApply);

        //文件信息填写
        int i1 = 0;
        List<LowairServiceFile> lowairServiceFiles = airspaceApplyAndFileDto.getLowairServiceFiles();
        if (CollUtil.isNotEmpty(lowairServiceFiles) ){
            for (LowairServiceFile lowairServiceFile : lowairServiceFiles) {
                TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
                BeanUtil.copyProperties(lowairServiceFile,tLowairServiceFile);
                //把空域表的ID存入文件表
                tLowairServiceFile.setFileId(IdUtil.randomUUID());
                tLowairServiceFile.setBusinessId(airspaceApply.getAirspaceApplyId());
                tLowairServiceFile.setCreateTime(DateUtils.getNowDate());
                i1 = tLowairServiceFileMapper.insert(tLowairServiceFile) + i1;
            }



        }
        return i+i1;
    }

    /**
     * 修改空域申请
     * 
     * @param airspaceApplyAndFileDto 空域申请 + 文件
     * @return 结果
     */
    @Override
    public int updateAirspaceApply(AirspaceApplyAndFileDto airspaceApplyAndFileDto)
    {
        //修改空域表
        AirspaceApply airspaceApply = airspaceApplyAndFileDto.getAirspaceApply();
        airspaceApply.setUpdateTime(DateUtils.getNowDate());
        int i = airspaceApplyMapper.updateAirspaceApply(airspaceApply);
        //修改文件表
        int i1 = 0;

//        if (CollUtil.isNotEmpty(airspaceApplyAndFileDto.getLowairServiceFiles()) ){
//            List<LowairServiceFile> lowairServiceFiles = airspaceApplyAndFileDto.getLowairServiceFiles();
//            for (LowairServiceFile lowairServiceFile : lowairServiceFiles) {
//                //把空域表的ID存入文件表
//                TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
//                BeanUtil.copyProperties(lowairServiceFile,tLowairServiceFile);
//                tLowairServiceFile.setBusinessId(airspaceApply.getAirspaceApplyId());
//                tLowairServiceFile.setUpdateTime(tLowairServiceFile.getCreateTime());
//                i1 = tLowairServiceFileMapper.insert(tLowairServiceFile) + i1;
//            }
//        }
        return  i+i1;

    }

    /**
     * 批量删除空域申请
     * 
     * @param airspaceApplyIds 需要删除的空域申请主键
     * @return 结果
     */
    @Override
    public int deleteAirspaceApplyByAirspaceApplyIds(String[] airspaceApplyIds)
    {
        for (String airspaceApplyId : airspaceApplyIds) {
            //通过业务ID删除所有底下文件
            LambdaQueryWrapper<TLowairServiceFile> qw = new LambdaQueryWrapper<>();
            qw.eq( null != airspaceApplyId,TLowairServiceFile::getBusinessId,airspaceApplyId);
           tLowairServiceFileMapper.delete(qw);
        }

        return airspaceApplyMapper.deleteAirspaceApplyByAirspaceApplyIds(airspaceApplyIds);
    }

    /**
     * 删除空域申请信息
     * 
     * @param airspaceApplyId 空域申请主键
     * @return 结果
     */
    @Override
    public int deleteAirspaceApplyByAirspaceApplyId(String airspaceApplyId)
    {
        LambdaQueryWrapper<TLowairServiceFile> qw = new LambdaQueryWrapper<>();
        qw.eq( null != airspaceApplyId,TLowairServiceFile::getBusinessId,airspaceApplyId);
        tLowairServiceFileMapper.delete(qw);
        return airspaceApplyMapper.deleteAirspaceApplyByAirspaceApplyId(airspaceApplyId);
    }
}